package com.dlc.shop.mp.common.platform.controller;

import cn.hutool.core.util.StrUtil;
import com.dlc.shop.common.response.ServerResponseEntity;
import com.dlc.shop.mp.common.model.WeixinKeywordresponse;
import com.dlc.shop.mp.common.service.WeixinKeywordresponseService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author lgh on 2019/01/18.
 */
@RestController
@RequestMapping("/weixin/keywordresponse")
@Tag(name = "平台端微信关键词回复接口")
@AllArgsConstructor
public class WeixinKeywordresponseController {

    private final WeixinKeywordresponseService weixinKeywordresponseService;

    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('weixin:keywordresponse:page')")
    @Operation(summary = "整页获取关键词回复")
    public ServerResponseEntity<List<WeixinKeywordresponse>> page() {
        List<WeixinKeywordresponse> list = weixinKeywordresponseService.listWeixinKeywordresponse();
        for (WeixinKeywordresponse weixinKeywordresponse : list) {
            if (StrUtil.isNotBlank(weixinKeywordresponse.getContent())) {
                weixinKeywordresponse.setContent(weixinKeywordresponse.getContent());
            }
        }
        return ServerResponseEntity.success(list);
    }

    @PostMapping
    @PreAuthorize("@pms.hasPermission('weixin:keywordresponse:save')")
    @Operation(summary = "保存关键词回复")
    public ServerResponseEntity<Void> save(@RequestBody @Valid WeixinKeywordresponse weixinKeywordresponse) {
        if (StrUtil.isNotBlank(weixinKeywordresponse.getContent())) {
            weixinKeywordresponse.setContent(weixinKeywordresponse.getContent());
        }
        weixinKeywordresponseService.save(weixinKeywordresponse);
        weixinKeywordresponseService.removeWeixinKeywordresponseCache();
        return ServerResponseEntity.success();
    }

    @PutMapping
    @PreAuthorize("@pms.hasPermission('weixin:keywordresponse:update')")
    @Operation(summary = "修改关键词回复")
    public ServerResponseEntity<Void> update(@RequestBody @Valid WeixinKeywordresponse weixinKeywordresponse) {
        if (StrUtil.isNotBlank(weixinKeywordresponse.getContent())) {
            weixinKeywordresponse.setContent(weixinKeywordresponse.getContent());
        }
        weixinKeywordresponseService.updateById(weixinKeywordresponse);
        weixinKeywordresponseService.removeWeixinKeywordresponseCache();
        return ServerResponseEntity.success();
    }

    @DeleteMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('weixin:keywordresponse:delete')")
    @Operation(summary = "根据id删除关键词回复")
    @Parameter(name = "id", description = "关键词回复id" , required = true)
    public ServerResponseEntity<Void> delete(@PathVariable Long id) {
        weixinKeywordresponseService.removeById(id);
        weixinKeywordresponseService.removeWeixinKeywordresponseCache();
        return ServerResponseEntity.success();
    }
}
